#include<cstdio>//uncle-lu
#include<algorithm>

int n;
int a[3010], b[3010];
int F[3010][3010];

int main()
{
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) 
		scanf("%d", &a[i]);
	for (int i = 1; i <= n; i++) 
		scanf("%d", &b[i]);

	for (int i = 1; i <= n; i++) 
	{
		int val = 0;
		if(b[0] < a[i])val = F[i-1][0];
		for (int j = 1; j <= n; j++) 
		{
			if(a[i] == b[j])
				F[i][j] = val + 1;
			else
				F[i][j] = F[i-1][j];

			if(b[j] < a[i] && val < F[i-1][j])
				val = F[i-1][j];
		}
	}

	int ans = 0;
	for (int i = 1; i <= n; i++) 
		if(F[n][i] > ans)
			ans = F[n][i];

	printf("%d", ans);
	return 0;
}
